package com.atguigu.gmall.common.config;

import com.atguigu.gmall.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 必须要保证启动类能扫描
 *
 * @author: atguigu
 * @create: 2023-06-06 09:38
 */
@Slf4j
@Configuration
@RestControllerAdvice//@ControllerAdvice + @ResponseBody
public class GlobalExceptionHandler {

    /**
     * 处理运行时异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    public Result runtimeException(RuntimeException e) {
        log.error("全局异常处理:{}", e);
        return Result.fail().message(e.getMessage());
    }


    /**
     * 处理运行时异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result exception(Exception e) {
        log.error("全局异常处理:{}", e);
        return Result.fail().message(e.getMessage());
    }

}
